import QtQuick
import QtQuick.Window
import QtQuick.Controls 2.5
import QtQuick.Particles 2.15

Item {
    id: root
    scale: 0.75
    anchors.fill: parent

    // 背景遮罩
    Rectangle {
        id: mask
        scale: 1 / 0.75
        anchors.fill: parent
        color: "black"
        opacity: 0.7
    }

    // 剑光效果
    Rectangle {
        id: swordLight
        width: parent.width * 2
        height: 100
        rotation: 45
        x: -parent.width
        y: parent.height
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#00ffffff" }
            GradientStop { position: 0.5; color: "#ffffffff" }
            GradientStop { position: 1.0; color: "#00ffffff" }
        }

        SequentialAnimation {
            running: true
            NumberAnimation {
                target: swordLight
                properties: "x,y"
                to: parent.width
                duration: 500
                easing.type: Easing.OutQuad
            }
            PauseAnimation { duration: 100 }
            ScriptAction {
                script: swordLight.visible = false
            }
        }
    }

    // 粒子系统
    ParticleSystem {
        id: particleSystem
        anchors.fill: parent
        running: true

        ImageParticle {
            source: "qrc:///image/system/particle/star.png"
            color: "#FFD700"
            colorVariation: 0.2
            rotation: 0
            rotationVariation: 45
            rotationVelocity: 15
            rotationVelocityVariation: 15
            entryEffect: ImageParticle.Scale
        }

        Emitter {
            anchors.fill: parent
            emitRate: 20
            lifeSpan: 1000
            lifeSpanVariation: 500
            size: 16
            sizeVariation: 8
            velocity: AngleDirection {
                angle: 90
                angleVariation: 360
                magnitude: 100
                magnitudeVariation: 50
            }
        }
    }

    // GIF动画
    AnimatedImage {
        id: animation
        source: "jianshan.gif"
        anchors.fill: parent
        asynchronous: true
        cache: false
    }

    // 一闪文字效果
    Text {
        id: skillText
        text: "一闪"
        anchors.centerIn: parent
        font.family: "Microsoft YaHei"
        font.pixelSize: 120
        color: "white"
        opacity: 0

        SequentialAnimation {
            running: true
            PauseAnimation { duration: 200 }
            ParallelAnimation {
                NumberAnimation {
                    target: skillText
                    property: "opacity"
                    from: 0
                    to: 1
                    duration: 300
                }
                NumberAnimation {
                    target: skillText
                    property: "scale"
                    from: 2
                    to: 1
                    duration: 300
                    easing.type: Easing.OutQuad
                }
            }
            PauseAnimation { duration: 1000 }
            NumberAnimation {
                target: skillText
                property: "opacity"
                to: 0
                duration: 500
            }
        }
    }

    // 动画控制
    Rectangle {
        property int frames: animation.frameCount
        property int gframes: animation.currentFrame

        width: 1
        height: 1
        x: (animation.width - width) * animation.currentFrame / frames
        y: animation.height
        color: "transparent"
        
        onGframesChanged: {
            if (animation.currentFrame === animation.frameCount || 
                animation.currentFrame === animation.frameCount - 1) {
                animation.playing = false
                roomScene.bigAnim.source = ""
                particleSystem.running = false
            }
        }
    }
}
